﻿using System;
using System.Linq;

namespace HCMS.Utils
{
    /// <summary>
    /// 随机数 工具类
    /// </summary>
    public class RandomUtil
    {
        private static Random random = new Random();

        /// <summary>
        /// 随机字符串
        /// </summary>
        /// <param name="chars">待处理的字符串。为空，则使用字母、数字随机生成</param>
        /// <param name="length">生成字符串长度</param>
        /// <returns></returns>
        public static string GetRandomString(string chars, int length)
        {
            if (string.IsNullOrWhiteSpace(chars))
            {
                for (char c = 'a'; c <= 'z'; c++)
                {
                    chars += c;
                }
                for (char c = 'A'; c <= 'Z'; c++)
                {
                    chars += c;
                }
                for (int i = 0; i <= 9; i++)
                {
                    chars += i;
                }
            }

            return new string(Enumerable.Repeat(chars, length)
                            .Select(s => s[random.Next(s.Length)]).ToArray());
        }
    }
}
